package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")
    public PageDTO<Item> PageSearch(@RequestParam ("page") Integer page,
                                      @RequestParam ("size") Integer size){

        Page<Item> result =itemService.page(new Page<>(page,size));
        log.info("分页查询:{},{},",page,size);
        return new PageDTO<>(result.getTotal(),result.getRecords());
    }

    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    @PostMapping
    public void add(@RequestBody Item item){
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(2);
        itemService.save(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable Long id ,@PathVariable Integer status){
        itemService.up(id,status);
    }

    @PutMapping
    public void update(@RequestBody Item item){

        item.setUpdateTime(new Date());
        item.setStatus(null);
        itemService.updateById(item);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        itemService.delete(id);

    }
}
